{include file="public/header,public/nav,public/fixbar" title="我的地址-<?php echo request()->pc_info['seo_title']?>"/}
<div class="layui-body">
    <div class="layui-container">
        <div class="layui-col-space15">
            <!-- 菜单栏 -->
            <div class="layui-col-xs12 layui-col-sm4 layui-col-md4 layui-col-lg3">
                <div class="layui-main user-main min-height-90vh">
                    {include file="user/menu"/}
                </div>
            </div>
            <div class="layui-col-xs12 layui-col-sm8 layui-col-md8 layui-col-lg9">
                <div class="layui-main user-main min-height-90vh">
                    <h2>收货地址</h2>
                    <div class="layui-table-tool">
                        <div class="layui-table-tool-temp"> 
                            <a href="JavaScript:;" class="layui-btn layui-bg-success layui-btn-sm add"><i class="layui-icon layui-icon-add-1"></i>新建地址</a>
                        </div>
                    </div>
                    <div class="layui-row">
                        <div class="layui-col-space15 address-list scrollbar height-700" id="address-list">
                            
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer"/}
<script type="text/javascript">
    layui.use(['flow','layer'], function(){
        var layer = layui.layer, flow = layui.flow, $=layui.jquery;
        
        $(function(){
            loadList();  // 加载列表
        })

        // 分页加载列表
        function loadList(){
            var limit = 10;//每次请求的条数
            flow.load({
                elem: '#address-list' //指定列表容器
                ,scrollElem: '#address-list'//滚动条所在元素
                ,end: '—— 已经到底了~ ——'
                ,done: function(page, next){ //到达临界点（默认滚动触发），触发下一页的回调
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        data: {'page': page, 'limit':limit},//请求的页码和每页显示条数
                        url: '/user/address',
                        success: function (res) {
                            var lis = [];
                            if(res.code == 0){
                                if (res.data.length > 0) {
                                    
                                    var newsHtml = '';
                                    layui.each(res.data, function(index, item){
                                        var status = '';
                                        if (item.default == 1) {
                                            status = `<span class="layui-badge layui-bg-red">默认</span>`;
                                        }
                                        newsHtml += `<div class="layui-col-xs12">
                                                        <div class="layui-panel" style="border: none;border-bottom: 1px solid #f2f2f2;">
                                                            <div class="address-info space-between">
                                                                <div>
                                                                    <h3>${item.region} ${item.address}</h3>
                                                                    <p class="align-items-center">
                                                                        <span>${item.name}，${item.phone}</span>
                                                                        ${status}
                                                                    </p>
                                                                </div>
                                                                <div>
                                                                    <a class="layui-btn layui-btn-primary layui-btn-sm edit" data-id="${item.id}"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                                                                    <a class="layui-btn layui-btn-primary layui-btn-sm delete" data-id="${item.id}"><i class="layui-icon layui-icon-delete"></i>删除</a>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>`;
                                    });
                                    
                                    lis.push(newsHtml);
                                    //执行下一页渲染，第二参数为：满足“加载更多”的条件，即后面仍有分页
                                    //res.total为Ajax返回的总页数，只有当前页小于总页数的情况下，才会继续出现加载更多
                                    next(lis.join(''), page < Math.ceil(res.count/limit));
                                    
                                } else {
                                    $('#address-list').html('<div class="layui-empty"><img src="/static/images/nodata/no_address.png"><p>暂无收货地址~</p></div>');
                                }
                            } else {
                                layer.msg(res.msg, {icon:2, time: 1500});
                            }
                        }
                        ,error:function(e){
                            layer.closeAll();
                            layer.msg("请求失败："+e.msg, {icon:2, time: 1500});
                        },
                    });
                }
            });
        }

        // 添加收货地址
        $(".add").click(function(){
            var index = layer.open({
                skin: 'layui-layer-radius', // 弹窗主题样式
                offset: 'auto',
                title: '新建收货地址',
                type: 2,
                anim: 'slideUp',
                shade: 0.3,
                shadeClose: true,
                area: ['620px', '500px'],
                content: '/user/address_edit'
            });
            var device = layui.device('mobile');
            if (device.mobile) {
                layer.full(index);
            }
        })

        // 编辑地址
        $(document).on('click', '.edit', function(){
            var id = $(this).data('id');
            var index = layer.open({
                skin: 'layui-layer-radius', // 弹窗主题样式
                offset: 'auto',
                title: '编辑收货地址',
                type: 2,
                anim: 'slideUp',
                shade: 0.3,
                shadeClose: true,
                area: ['620px', '500px'],
                content: '/user/address_edit?id='+id
            });
            var device = layui.device('mobile');
            if (device.mobile) {
                layer.full(index);
            }
        })

        // 删除地址
        $(document).on('click', '.delete', function(){
            var id = $(this).data('id');
            layer.confirm('确定删除该地址吗？', {
                skin: 'layui-layer-radius', // 弹窗主题样式
                title : '操作确认',
            },function(index){
                var loading = layer.msg('数据删除中..', {icon: 16,shade: 0.3,time: false});
                $.ajax({
                    url:'/user/address_delete',
                    type:'delete',
                    data:{'id':id},
                    dataType:"JSON",
                    headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                    success:function(data){
                        layer.close(loading);  //返回数据关闭loading
                        if(data.code == 0){
                            layer.msg(data.msg,{icon: 1,time:1500},function(){
                                location.reload();
                            });
                        }
                        else{
                            layer.msg(data.msg,{icon: 2});
                        }
                    },
                    error:function(e){
                        layer.msg('请求失败'+e.responseText,{icon: 2});
                    },
                    
                });
            });
        })
    });

</script>